﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Service;
using Model;
using Manager.Helper;
namespace Manager
{
    public class ResultManager
    {
        ResultService service = new ResultService();
        public DataTable Search(int pageIndex,int pageSize,out int count,string tjnd, string name,string filed, string districtid, string school,bool merge)
        {
            using(ResultService ws = new ResultService())
            {
                if(string.IsNullOrEmpty(name))
                    return ws.Search(pageIndex, pageSize, out count, tjnd, districtid, school, merge);
                else
                    return ws.Search(pageIndex, pageSize, out count, tjnd, name, filed, districtid, school, merge);
            }
        }
        public DataTable NoPageSearch(out int count, string tjnd, string name, string filed, string districtid, string school, bool merge)
        {
            using (ResultService ws = new ResultService())
            {
                if (string.IsNullOrEmpty(name))
                    return ws.NoPageSearch(out count, tjnd, districtid, school, merge);
                else
                    return ws.NoPageSearch(out count, tjnd, name, filed, districtid, school, merge);
            }
        }
        public DataTable Search(int pageIndex, int pageSize, out int count, string tjnd, string name, string filed)
        {
            using (ResultService ws = new ResultService())
            {
                if (string.IsNullOrEmpty(name))
                    return ws.Search(pageIndex, pageSize, out count, tjnd);
                else
                    return ws.SearchCondition(pageIndex, pageSize, out count, tjnd, name, filed);
            }
        }
        public DataTable NoPageSearch(out int count, string tjnd, string name, string filed)
        {
            using (ResultService ws = new ResultService())
            {
                if (string.IsNullOrEmpty(name))
                    return ws.NoPageSearch(out count, tjnd);
                else
                    return ws.NoPageSearchCondition(out count, tjnd, name, filed);
            }
        }

        public DataTable StudentSearch(int pageIndex, int pageSize, out int count, string tjnd, string name, string filed, string districtid, string school)
        {
            using (ResultService ws = new ResultService())
            {
                if (string.IsNullOrEmpty(name))
                    return ws.StudentSearch(pageIndex, pageSize, out count, tjnd, districtid, school);
                else
                    return ws.StudentSearch(pageIndex, pageSize, out count, tjnd, name, filed, districtid, school);
            }
        }
        public DataTable StudentSearch(int pageIndex, int pageSize, out int count, string tjnd, string name, string filed)
        {
            using (ResultService ws = new ResultService())
            {
                if (string.IsNullOrEmpty(name))
                    return ws.StudentSearch(pageIndex, pageSize, out count, tjnd);
                else
                    return ws.StudentSearchCondition(pageIndex, pageSize, out count, tjnd, name, filed);
            }
        }
        public DataTable GetByStudentId(string tjnd, string studentid)
        {
            using (ResultService ws = new ResultService())
            {
               return  ws.GetByStudentId(tjnd, studentid);
            }
        }
        public List<APIStudent> GetAPIStudent(string studentid)
        {
            using (ResultService ws = new ResultService())
            {
                for (int year = DateTime.Now.Year; year > 2008; year--)
                {
                    var templist = ws.GetAPIStudent(year.ToString(), studentid);
                    if (templist.Count > 0)
                        return templist;
                }
                return null;
            }
        }
        public HW_Result2013 GetModelStudent(string tjnd, string studentid)
        {
            using (ResultService ws = new ResultService())
            {
              return ws.GetModelStudent(tjnd, studentid);
            }
        }

        public HW_Result2013 GetResult(HW_Result2013 result)
        {
            //if (result.LSY == "0" && result.RSY == "0")
            //    result.SSYPJ = "无";
            //if((result.LSY=="1"||result.RSY=="1")&&result.LSY!="2"&&result.RSY!="2")
            //    result.SSYPJ = "可疑";
            //if(result.LSY=="2"||result.RSY=="2")
            //    result.SSYPJ = "有沙";
            if ((result.LSL.HasValue && (double)result.LSL.Value < 5.0) || (result.RSL.HasValue && (double)result.RSL.Value < 5.0))
                result.SLPJ = "视力低下";
            if ((result.LSL.HasValue && (double)result.LSL.Value >=5.0) || (result.RSL.HasValue && (double)result.RSL.Value >= 5.0))
                result.SLPJ = "正常";
            return result;
        }
        public HW_Result2013 Translate(HW_Result2013 result)
        {
            System.Reflection.PropertyInfo[] myPropertyInfo = result.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
            HW_ItemResult temp = null;
            List<string> items = ResultDicCache.ItemDic.GroupBy(p => p.EngLishItem).Select(p=>p.Key).ToList() ;
            int r;
            foreach(var m in myPropertyInfo)
            {
                if (items.Contains(m.Name))
                {
                    string str = m.GetValue(result, null) == null ? "100" : m.GetValue(result, null).ToString();
                    if (int.TryParse(str, out r))
                    {
                        temp = ResultDicCache.ItemDic.FirstOrDefault(p => p.EngLishItem == m.Name && p.Item == r);
                        if (temp != null)
                        {
                            m.SetValue(result, temp.Result, null);
                        }
                    }
                }
            }
            return result;
        }

        public DataTable GetSelfResult(string tjnd, string studentid)
        {
            using (ResultService ws = new ResultService())
            {
                return ws.GetSelfResult(tjnd, studentid);
            }
        }
        public DataTable GetSelfVaccin(string tjnd, string studentid)
        {
            using (ResultService ws = new ResultService())
            {
                return ws.GetSelfVaccin(tjnd, studentid);
            }
        }
        public void CountYearResult()
        {
            try
            {
                using (ResultService ws = new ResultService())
                {
                    ws.CountYearResult();
                }
            }
            catch
            {
            }
        }
        public DataSet GetCountYearResult()
        {
            try
            {
                using (ResultService ws = new ResultService())
                {
                    return ws.GetCountYearResult();
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
        public DataSet GetBySchoolCode(string tjnd, string schoolCode)
        {
            using (ResultService ws = new ResultService())
            {
                return ws.GetBySchoolCode(tjnd, schoolCode);
            }
        }
    }
}
